﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.storage.modes {
    import flash.events.*;
    import Classes.*;
    import Classes.util.*;
    import GameMode.*;
    import Modules.storage.*;
    import Engine.*;
    import Modules.storage.transactions.*;

    public class GMPlaceFromStorage extends GMObjectMove {

        protected var m_storageType:StorageType;
        protected var m_storageKey:String;
        protected var m_storageBin:BaseStorageUnit;
        protected var m_itemName:String;
        protected var m_itemClass:Class;

        public function GMPlaceFromStorage(_arg1:StorageType, _arg2:String, _arg3:String, _arg4:Class=null){
            this.m_storageType = _arg1;
            this.m_storageKey = _arg2;
            this.m_storageBin = Global.player.storageComponent.getStorageUnit(_arg1, _arg2);
            this.m_itemName = _arg3;
            this.m_itemClass = _arg4;
            super(null, null, null, Constants.INDEX_NONE, true);
        }
        override public function onMouseMove(_arg1:MouseEvent):Boolean{
            var _local2:Boolean;
            if (!m_selectedObject){
                m_highlightedPoint = getMouseTilePos();
                m_selectedObject = this.getItemFromStorage();
                m_selectedObject.setOuter(Global.world);
                resetOnPlaceCollisions();
                registerAllOnPlaceCollisions();
                snapPoint(m_highlightedPoint);
                m_selectedObject.setPosition(m_highlightedPoint.x, m_highlightedPoint.y);
            };
            Global.world.citySim.roadManager.updateResource((m_selectedObject as MapResource));
            m_autoRotate = (m_selectedObject as MapResource).autoRotate;
            if (m_autoRotate){
                rotateToRoad((m_selectedObject as MapResource));
            };
            _local2 = super.onMouseMove(_arg1);
            return (_local2);
        }
        override protected function completeObjectDrop():void{
            var _local3:Object;
            var _local1:MapResource = (m_selectedObject as MapResource);
            var _local2:Boolean = _local1.isPlacedObjectNonBuilding();
            if (m_releasedPoint){
                _local3 = new Object();
                _local3.x = Math.round(m_releasedPoint.x);
                _local3.y = Math.round(m_releasedPoint.y);
                _local3.state = _local1.getState();
                _local3.direction = _local1.getDirection();
                _local3.storageType = this.m_storageType.type;
                _local3.storageKey = this.m_storageKey;
                _local3 = _local1.addTMoveParams(_local3);
                this.m_storageBin.remove((m_selectedObject as ItemInstance));
                GameTransactionManager.addTransaction(new TMoveFromStorage(_local1, _local3));
                _local1.restoreFromStorage();
            };
            m_selectedObject.setPosition(Math.round(m_releasedPoint.x), Math.round(m_releasedPoint.y));
            m_selectedObject.conditionallyReattach();
            m_selectedObject.onObjectDrop(m_objectStartPos);
            m_objectPlacedSuccess = true;
            if (_local2){
                Sounds.play("click4");
            } else {
                Sounds.play("place_building");
            };
            onObjectDropCompleted();
        }
        protected function getItemFromStorage():MapResource{
            var _local1:ItemInstance;
            if (this.m_storageBin){
                _local1 = this.m_storageBin.getItemsByName(this.m_itemName)[0];
            };
            return ((_local1 as MapResource));
        }
        protected function removeItemFromStorage():void{
            this.m_storageBin.remove((m_selectedObject as ItemInstance));
        }

    }
}//package Modules.storage.modes 
